package top

import (
	"github.com/lzzyuhyy/framework/mysql"
	"gorm.io/gorm"
)

type Data struct {
	UserId   int64  `json:"user_id"`
	Count    int64  `json:"count"`
	Sum      int64  `json:"sum"`
	Nickname string `json:"nickname"`
	Avatar   string `json:"avatar"`
}

// OverallRanking 排行榜总榜
func OverallRanking() ([]Data, error) {
	var info []Data
	var top int = 0
	err := mysql.CreateMySQLClient(func(db *gorm.DB) error {
		if top == 0 {
			db.Raw("select user_id,nickname,avatar, count(interviews_id) as `count` from `user_answers` JOIN `users` on  users.id = user_answers.user_id group by user_id order by `count` desc").Scan(&info)
		}
		return nil
	})
	return info, err
}

// GraspRanking 掌握榜
func GraspRanking() ([]Data, error) {
	var info []Data
	err := mysql.CreateMySQLClient(func(db *gorm.DB) error {
		db.Raw("select user_id,nickname,avatar, count(interviews_id) as `count` from `user_answers` JOIN `users` on  users.id = user_answers.user_id WHERE is_grasps =1 group by user_id order by `count` desc").Scan(&info)
		return nil
	})
	return info, err
}

// LengthTimeRanking  时长榜
func LengthTimeRanking() ([]Data, error) {
	var info []Data
	err := mysql.CreateMySQLClient(func(db *gorm.DB) error {
		db.Raw("SELECT user_id,users.nickname,avatar,SUM(sum_time) as `sum` FROM `users` JOIN `record_swipe_times` AS rec on  users.id = rec.user_id group by user_id order by `sum` desc").Scan(&info)
		return nil
	})
	return info, err
}

// CategorizationRanking  分类榜
func CategorizationRanking(id int) ([]Data, error) {
	var info []Data
	err := mysql.CreateMySQLClient(func(db *gorm.DB) error {
		if id == 0 {
			id = 1
		}
		db.Raw("SELECT user_id,nickname,avatar,COUNT(interviews_id) AS `count` FROM `user_answers` JOIN `users` on  users.id = user_answers.user_id WHERE p_id = ? GROUP BY user_id order by `count` desc", id).Scan(&info)
		return nil
	})
	return info, err
}
